home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 101 / CD-ROM 101.iso / compl / maya5ple / Install_MayaPLE5_English.exe / Maya / Data1.cab / Maya_phong_postlighting_frag < prev    next >
Encoding:
Text File  |  2003-07-17  |  7.5 KB  |  231 lines

  1. // phong_fragment.cg
  2. //
  3. // This file cannot be directly compiled through CG... it first must be
  4. // parsed to insert channel-specific instructions.
  5. //
  6. // For example:
  7. // cgc.exe "$(InputPath)" -profile fp30 -o $(InputName).fp -DSPOT_LIGHT -DTRANSP_IN_TRANSP_RGB -DPROJ_LIGHT_TEXTURE
  8.  
  9. struct vert2frag
  10. {
  11.     float4 hPosition            : POSITION;
  12.  
  13. #if defined(BUMP_MAP)
  14.     float3 wBinormal            : TEXCOORD0;
  15.     float3 wTangent                : TEXCOORD1;
  16.     float4 bumpTexUv            : TEXCOORD2;
  17. #else
  18.     float3 wNormal                : TEXCOORD0;
  19. #endif // BUMP_MAP
  20.     
  21.     float4 transpTexUv            : TEXCOORD3;
  22.     float4 incanTexUv            : TEXCOORD4;
  23.     float4 reflectivityTexUv    : TEXCOORD5;
  24.     float4 wPosition            : TEXCOORD6;
  25. };
  26.  
  27. float4 main(vert2frag        IN,
  28.             uniform float4  wEyePosition,
  29.             uniform float    normalMultiplier,
  30.  
  31. #if defined(BUMP_MAP)
  32.             uniform sampler2D bumpTex,
  33.             uniform float4    bumpTexMatrix0,
  34.             uniform float4    bumpTexMatrix1,
  35. #endif // BUMP_MAP
  36.  
  37.             //[claforte] May one day be used to mimic software.
  38.             //uniform sampler2D specTex,
  39.             //uniform float4    specTexMatrix0,
  40.             //uniform float4    specTexMatrix1,
  41.             
  42.             uniform sampler2D transpTex,
  43.             uniform float4    transpTexMatrix0,
  44.             uniform float4    transpTexMatrix1,
  45.  
  46.             uniform sampler2D incanTex,
  47.             uniform float4    incanTexMatrix0,
  48.             uniform float4    incanTexMatrix1,
  49.  
  50. #if defined(REFLECT_CUBE_LOOKUP) || defined(REFRACT_CUBE_LOOKUP)
  51.             uniform float3 worldToCubeRotationMatrix0,
  52.             uniform float3 worldToCubeRotationMatrix1,
  53.             uniform float3 worldToCubeRotationMatrix2,
  54.             uniform samplerCUBE reflectedColTex,
  55. #endif // (REFLECT_CUBE_LOOKUP) || (REFRACT_CUBE_LOOKUP)
  56.  
  57. #if defined(REFLECT_SPHERE_LOOKUP)
  58.             uniform sampler2D reflectedColTex,
  59. #endif
  60.  
  61. #if defined (REFRACT_CUBE_LOOKUP)
  62.             // Refractive indices for red, green, blue.
  63.             uniform float3 refractiveIndices,
  64. #endif            
  65.  
  66.             uniform sampler2D reflectivityTex,
  67.             uniform float4    reflectivityTexMatrix0,
  68.             uniform float4    reflectivityTexMatrix1) : COLOR
  69.     // Calculate view direction in world space,
  70.     // from point to eye.
  71.     //
  72.     float3 wViewDir = normalize(wEyePosition.xyz - IN.wPosition.xyz);
  73.  
  74. #if defined(BUMP_MAP)
  75.     // Compute an orthonomal basis to transform from tangent space
  76.     // to world space, and vice-versa.
  77.     //
  78.     float3 wTangent = normalize(IN.wTangent);
  79.     float3 wNormal = normalize(cross(wTangent, IN.wBinormal));
  80.     float3 wBinormal = cross(wNormal, wTangent);
  81.  
  82.     // Translate and scale bump texture coordinates, by multiplying
  83.     // the uvs by the bump texture matrix.
  84.     //
  85.     float2 placedBumpTexUv;
  86.     float4 bumpTexUv = float4(IN.bumpTexUv.xy, 0.0, 1.0);
  87.     placedBumpTexUv.x = dot(bumpTexMatrix0, bumpTexUv);
  88.     placedBumpTexUv.y = dot(bumpTexMatrix1, bumpTexUv);
  89.  
  90.     // Fetch the bumped normal (in tangent space) from the bump texture.
  91.     // The (... * 2 - 1) at the end is used to unpack the normal components,
  92.     // from the [0,1] to [-1, -1] range.
  93.     //
  94.     float3 tBumpedNormal = normalize(f3tex2D(bumpTex, placedBumpTexUv) * 2 - 1);
  95.  
  96.     // Compute the world-space bumped normal.
  97.     //
  98.     float3 wBumpedNormal = normalMultiplier * tBumpedNormal.x * wTangent + 
  99.                            normalMultiplier * tBumpedNormal.y * wBinormal + 
  100.                            tBumpedNormal.z * wNormal;
  101. #else  // !defined(BUMP_MAP)
  102.     float3 wNormal = normalize(IN.wNormal);
  103.     float3 wBumpedNormal = wNormal;
  104. #endif // BUMP_MAP
  105.  
  106.  
  107.     // To support transparency and back-face lighting,
  108.     // allow normal inversion.
  109.  
  110.     wNormal = normalMultiplier * wNormal;
  111.     wBumpedNormal = normalMultiplier * wBumpedNormal;
  112.  
  113.     // Compute the viewer's reflection direction.
  114.     //
  115.     float3 wReflectDir = reflect(-wViewDir, wBumpedNormal);
  116.     
  117.  
  118. #if defined(REFLECT_CUBE_LOOKUP)
  119.  
  120.     // Transform the reflection vector from world space to
  121.     // cube space. Ideally, we'd take the full cube placement
  122.     // information into account, but since this is quite
  123.     // complicated, we use the traditional OpenGL-shortcut of 
  124.     // only applying a rotation.
  125.     //
  126.     float3 cReflectDir;
  127.     cReflectDir.x = dot(wReflectDir, worldToCubeRotationMatrix0);
  128.     cReflectDir.y = dot(wReflectDir, worldToCubeRotationMatrix1);
  129.     cReflectDir.z = dot(wReflectDir, worldToCubeRotationMatrix2);
  130.     float3 reflectedColor = f3texCUBE(reflectedColTex, cReflectDir);
  131.  
  132. #elif defined(REFLECT_SPHERE_LOOKUP)
  133.  
  134.     // This calculation is directly taken from the OpenGL spec's sphere
  135.     // mapping section.
  136.     //
  137.     float x = wReflectDir.x;
  138.     float y = wReflectDir.y;
  139.     float z = wReflectDir.z + 1;
  140.     float m = 2.0 * sqrt(x * x + y * y + z * z);
  141.  
  142.     float2 sphereUv = wReflectDir.xy / m + 0.5;
  143.     float3 reflectedColor = f3tex2D(reflectedColTex, sphereUv);
  144.  
  145. #else
  146.  
  147.     // Set the reflected color to black.
  148.     float3 reflectedColor = float3(0, 0, 0);
  149.  
  150. #endif
  151.  
  152.     // Apply the reflectivity texture matrix to the corresponding texture 
  153.     // coordinates, then fetch the appropriate texel.
  154.     //
  155.     float2 placedReflectivityTexUv;
  156.     float4 reflectivityTexUv = float4(IN.reflectivityTexUv.xy, 0.0, 1.0);
  157.     placedReflectivityTexUv.x = dot(reflectivityTexMatrix0, reflectivityTexUv);
  158.     placedReflectivityTexUv.y = dot(reflectivityTexMatrix1, reflectivityTexUv);
  159.     float reflectivity = f4tex2D(reflectivityTex, placedReflectivityTexUv).r;
  160.  
  161.     // Apply the incandescence texture matrix to the corresponding texture 
  162.     // coordinates, then fetch the appropriate texel.
  163.     //
  164.     float2 placedIncanTexUv;
  165.     float4 incanTexUv = float4(IN.incanTexUv.xy, 0.0, 1.0);
  166.     placedIncanTexUv.x = dot(incanTexMatrix0, incanTexUv);
  167.     placedIncanTexUv.y = dot(incanTexMatrix1, incanTexUv);
  168.     float3 incandescence = f3tex2D(incanTex, placedIncanTexUv);
  169.  
  170.     float3 refractedColor = 0;
  171.  
  172. #if defined(REFRACT_CUBE_LOOKUP)
  173.  
  174.     float4 opacityColor;
  175.  
  176. #if defined(OPACITY_IN_COLOR_ALPHA)
  177.     // The opacity is encoded in the base color texture's alpha.
  178.     // (NOTE: the cpp code is expected to bind the color_tex texture
  179.     // to the transpTex sampler2D.)
  180.     //
  181.     opacityColor = fetchedTranspColor.aaaa;
  182.  
  183. #else    // TRANSP_IN_TRANSP_RGB
  184.     // Apply the transparency texture matrix to the corresponding texture 
  185.     // coordinates, then fetch the appropriate texel.
  186.     //
  187.     float2 placedTranspTexUv;
  188.     float4 transpTexUv = float4(IN.transpTexUv.xy, 0.0, 1.0);
  189.     placedTranspTexUv.x = dot(transpTexMatrix0, transpTexUv);
  190.     placedTranspTexUv.y = dot(transpTexMatrix1, transpTexUv);
  191.     float4 fetchedTranspColor = f4tex2D(transpTex, placedTranspTexUv);
  192.  
  193.     // The transparency color is encoded in the transparency texture's RGB,
  194.     // and the opacity is stored in the transparency texture's alpha.
  195.     //
  196.     opacityColor.rgb = 1-fetchedTranspColor.rgb;
  197.     opacityColor.a = fetchedTranspColor.a;
  198. #endif // 
  199.     
  200.     // Compute the refracted color.
  201.     float3 cViewDir;
  202.     cViewDir.x = dot(-wViewDir, worldToCubeRotationMatrix0);
  203.     cViewDir.y = dot(-wViewDir, worldToCubeRotationMatrix1);
  204.     cViewDir.z = dot(-wViewDir, worldToCubeRotationMatrix2);
  205.  
  206.     float3 cBumpedNormal;
  207.     cBumpedNormal.x = dot(wBumpedNormal, worldToCubeRotationMatrix0);
  208.     cBumpedNormal.y = dot(wBumpedNormal, worldToCubeRotationMatrix1);
  209.     cBumpedNormal.z = dot(wBumpedNormal, worldToCubeRotationMatrix2);
  210.  
  211.     // TODO: implement and test chromatic abherration.
  212.     //
  213.     float3 cRefractDir = refract(cViewDir, cBumpedNormal, refractiveIndices.r);
  214.     refractedColor = f3texCUBE(reflectedColTex, cRefractDir);
  215.  
  216.     // Modulate the refracted color by the transparency.
  217.     // (ie: a highly transparent surface will translate into a highly
  218.     // refractive surface.)
  219.     //
  220.     refractedColor.rgb = refractedColor * (1-opacityColor.rgb);
  221.  
  222. #endif // REFRACT_CUBE_LOOKUP
  223.  
  224.     float4 finalColor;
  225.     finalColor.rgb = reflectivity * reflectedColor + incandescence + refractedColor;
  226.     finalColor.a = 1;
  227.  
  228.     return finalColor;
  229. }
  230.